#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),right(right) {}
};
 
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        if (!root)
            return res;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            for (int i = 0; i < size; ++i) {
                TreeNode* node = q.front();
                q.pop();
                if (i == size - 1) {
                    res.push_back(node->val);
                }
                if (node->left)
                    q.push(node->left);
                if (node->right)
                    q.push(node->right);
            }
        }
        return res;
    }
};


// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};


class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        if (!root)
        {
            return{};
        }
        vector<vector<int>>ans;
        queue<Node*>q;
        q.push(root);
        while (!q.empty())
        {
            int cnt = q.size();
            vector<int>level;
            for (int i = 0; i < cnt; ++i)
            {
                Node* cur = q.front();
                q.pop();
                level.push_back(cur->val);
                for (Node* child : cur->children)
                {
                    q.push(child);
                }
            }
            ans.push_back(move(level));
        }
        return ans;

    }
};